package org.lionel.xml;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

public class Dom4jTest {

    public static void main(String[] args) throws SAXException, DocumentException {
        Dom4jTest t = new Dom4jTest();
        SAXReader saxReader = new SAXReader();
        // 读取文件
        Document read = saxReader.read(t.getClassPath("user.xml"));
        // 获取根节点
        Element rootElement = read.getRootElement();
        getNodes(rootElement);
    }
    // 1. 通过文件名获取输入流
    public InputStream getClassPath(String xmlPath) {
        InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(xmlPath);
        return resourceAsStream;
    }
    // 2. 获取节点信息
    public static void getNodes(Element rootElement) {
        System.out.println("获取当前名称:" + rootElement.getName());
        // 获取属性信息
        List<Attribute> attributes = rootElement.attributes();
        for (Attribute attribute : attributes) {
            System.out.println("属性:" + attribute.getName() + " : " + attribute.getText());
        }
        // 获取属性value
        String value = rootElement.getTextTrim();
        if (!StringUtils.isEmpty(value)) {
            System.out.println("value:" + value);
        }
        // 使用迭代器遍历,继续遍历子节点
        Iterator<Element> elementIterator = rootElement.elementIterator();
        while (elementIterator.hasNext()) {
            Element next = elementIterator.next();
            getNodes(next);
        }
    }
}
